const gulp = require("gulp");
const less = require("gulp-less");
const htmlMin = require("gulp-htmlmin");
const cssMin = require("gulp-cssmin");
const uglify = require("gulp-uglify");
const imageMin = require("gulp-imagemin");
const clean = require("gulp-clean");
const pump = require("pump");
const nodemon = require("gulp-nodemon");
const browserify = require("browserify");
const watchify = require("watchify");
const babelify = require("babelify");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const fs = require("fs");

/*----------开发环境：复制html, css, img, js到指定目录-----------*/
gulp.task("copy", function() {
    //复制html
    gulp.src("src/html/**/*.html")
        .pipe(gulp.dest("app/views/"));

    gulp.src("src/html/public/*.html")
        .pipe(gulp.dest("static/views/"));
        
    gulp.src("src/html/h5/common/**/*.html")
        .pipe(gulp.dest("static/views/h5/"));    
    //复制css
    gulp.src("src/css/**/*.css")
        .pipe(gulp.dest("static/css/"));
    //复制img
    gulp.src("src/img/**/*")
        .pipe(gulp.dest("static/img/"));  
    //复制filters.js
    console.log("-----复制filters.js----");
    gulp.src("src/js/filters.js")
        .pipe(gulp.dest("app/modules/"));  
});

/*----------生成环境：压缩html到指定目录-----------*/
gulp.task("htmlmin", function() {
    let options = {
        removeComments: true, //清除注释
        collapseWhitespace: true, //压缩html
        collapseBooleanAttributes: true, //省略布尔属性
        removeEmptyAttributes: true, //删除空格属性
        removeScriptTypeAttributes: true, //清除script的type属性
        removeStyleTypeAttribues: true, // 清除link中type属性
        minifyJS: true, //压缩页面js
        minifyCSS: true //压缩页面css
    };
    gulp.src("src/html/**/*.html")
        .pipe(htmlMin(options))
        .pipe(gulp.dest("app/views/"));
});


/*--------开发环境：编译less文件---------*/
gulp.task("less", function() {
    gulp.src("src/less/**/*.less")
        .pipe(less())
        .pipe(gulp.dest("static/css/"))
});

/*--------生产环境：编译less文件并压缩-----------*/
gulp.task("less_min", function() {
    gulp.src("src/less/**/*.less")
        .pipe(less())
        .pipe(cssMin())
        .pipe(gulp.dest("static/css/"));
});

/*--------生产环境：压缩css文件----------------*/
gulp.task("css_min", function() {
    gulp.src("src/css/**/*.css")
        .pipe(cssMin())
        .pipe(gulp.dest("static/css//"));
});

/*--------生产环境：压缩图片---------------*/
gulp.task("image_min", function() {
    gulp.src(["src/img/**/*.jpg", "src/img/**/*.png"])
        .pipe(imageMin())
        .pipe(gulp.dest("static/img"));
});

/*--------生产环境：清除文件--------------*/
gulp.task("clean", function() {
    gulp.src(["app/views", "static/css", "static/img"])
        .pipe(clean({ force: true }));
});

/*--------开发环境：检测html,css,img变化------------*/
gulp.task("watch", function() {
    gulp.watch("src/less/**/*.less", ["less"]);
    gulp.watch([
        "src/html/**/*.html",
        "src/css/**/*.css",
        "src/img/**/*",
        "src/js/filters.js"
    ], ["copy"]);
});

/*--------启动服务器----------*/
gulp.task("server", function() {
    nodemon({
        script: "bin/www",
        ext: "js",
        ignore: ["app/views/", "static/css/", "static/js/", "static/img/", "src/js/"],
        env: { "NODE_ENV": "development" },
        // tasks: ["copy", "less"]
    });
});

const jsFiles = [
    "src/js/index.js",
    "src/js/login.js",
    "src/js/product_detail.js",
    "src/js/order_confirm.js",
    "src/js/success.js",
    "src/js/factory_entry_confirm.js",
    "src/js/process.js",
    "src/js/order_list.js",
    "src/js/h5/success.js",
    "src/js/h5/order_list.js",
    "src/js/filters.js",
    "src/js/nunjucks.config.js",
    "src/js/h5/product_list.js",
    "src/js/h5/login.js",
    "src/js/modules/modal.js",
    "src/js/h5/factory_entry.js",
    "src/js/h5/product_detail.js",
    "src/js/h5/order_confirm.js",
    "src/js/h5/address_list.js",
    "src/js/h5/index.js"
];
/*-------browserify--------------*/
gulp.task("browserify", function() {
    jsFiles.map((item) => {
        let b = browserify({
            entries: item,
            cache: {},
            packageCache: {},
            plugin: [watchify],
            transform: [
                ["babelify", {
                    presets: ["es2015"]
                }]
            ]
        });
        bundle();

        function bundle() {
            b.bundle().pipe(fs.createWriteStream(item.replace("src", "static")));
        };
        b.on("update", function() {
            console.log("<-----更新js代码------>");
            bundle();
        });
    });
});

gulp.task("uglify", function(){
    jsFiles.map(item=>{
        browserify({
            entries: item,
            transform: [
                ["babelify", {
                    presets: ["es2015"]
                }]
            ]
        })
        .bundle()
        .pipe(source(item.replace("src/js/", "")))
        .pipe(buffer())
        .pipe(uglify())
        .pipe(gulp.dest("static/js/"));
    });
});




/*--------默认---------*/
gulp.task("start", ["copy", "watch", "browserify", "server"]);

gulp.task("build", ["htmlmin", "less_min", "css_min", "image_min", "uglify"]);


